package com.stu.manager.scoreinfo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.manager.config.Result;
import com.stu.manager.course.entity.SysCourse;
import com.stu.manager.handler.LoginHandler;
import com.stu.manager.personnel.entity.SysStudent;
import com.stu.manager.personnel.entity.dto.StudentDto;
import com.stu.manager.scoreinfo.entity.SysScore;
import com.stu.manager.scoreinfo.entity.dto.CourseAndStuDto;
import com.stu.manager.scoreinfo.entity.dto.ScoreInfoDto;
import com.stu.manager.scoreinfo.entity.vo.CourseVo;
import com.stu.manager.scoreinfo.service.SysScoreServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-02-16
 */
@RestController
@RequestMapping("/api/score")
@CrossOrigin
public class SysScoreController {
    @Autowired
    private SysScoreServiceImpl sysScoreService;
    @Autowired
    private LoginHandler loginHandler;
    @GetMapping("getTeacherCourse")
    @ApiOperation("获取老师的所有年级所有课程")
    public Result getTeacherCourse(HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        String jobNum = request.getHeader("X-Token");
        HashMap<String, List> teacherCourse = sysScoreService.getTeacherCourse(jobNum);
        return Result.ok().data("coursesAndGrade",teacherCourse);
    }

    @PostMapping("pageStudent/{current}/{limit}")
    @ApiOperation("查询加分页展示学生")
    public Result pageStudent(@PathVariable Long current,
                            @PathVariable Long limit,
                            @RequestBody CourseVo courseVo,
                              HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        String courseName = courseVo.getCourseName();
        Integer classGrade = courseVo.getClassGrade();
        String jobNum = request.getHeader("X-Token");

        Page<SysStudent> pageStudent = new Page<>(current, limit);
        if (StringUtils.isEmpty(courseName)) {
            return Result.error().message("请选课程！");
        }
        if (StringUtils.isEmpty(classGrade)) {
            return Result.error().message("请选择年级！");
        }
        Result result = sysScoreService.pageStudent(pageStudent, classGrade, courseName, jobNum);

        return result;
    }


    @PostMapping("getScoreInfo")
    @ApiOperation("查询学生是否成绩录入")
    public Result getScoreInfo(@RequestBody ScoreInfoDto scoreInfoDto,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result scoreInfo = sysScoreService.getScoreInfo(scoreInfoDto);

        return scoreInfo;
    }


    @PostMapping("inputScore")
    @ApiOperation("成绩输入")
    public Result inputScore(@RequestBody SysScore sysScore,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        System.out.println(sysScore);
        Result result = sysScoreService.inputScore(sysScore);
        return result;
    }

    @PostMapping("importStudentToScore")
    @ApiOperation("将课程下所有学生信息导入成绩表，成绩为0")
    public Result importStudentToScore(@RequestBody CourseVo courseVo,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        String jobNum = request.getHeader("X-Token");
        sysScoreService.importStudentToScore(courseVo,jobNum);
        return Result.ok().message("当前年级学生已初始化!");
    }

    @PostMapping("pageScore/{current}/{limit}")
    @ApiOperation("查询加分页展示学生成绩信息")
    public Result pageScore(@PathVariable Long current,
                              @PathVariable Long limit,
                              @RequestBody CourseVo courseVo,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        String courseName = courseVo.getCourseName();
        Integer classGrade = courseVo.getClassGrade();

        Page<SysScore> pageScore = new Page<>(current, limit);
        if (StringUtils.isEmpty(courseName)) {
            return Result.error().message("请选课程！");
        }
        if (StringUtils.isEmpty(classGrade)) {
            return Result.error().message("请选择年级！");
        }
        Result result = sysScoreService.pageScore(pageScore, classGrade, courseName);

        return result;
    }


    @PostMapping("createCharts")
    @ApiOperation("生成课程学生的几个与不及格比例图表")
    public Result createCharts(@RequestBody CourseVo courseVo,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        String jobNum = request.getHeader("X-Token");
        String courseName = courseVo.getCourseName();
        Integer classGrade = courseVo.getClassGrade();

        if (StringUtils.isEmpty(courseName)) {
            return Result.error().message("请选课程！");
        }
        if (StringUtils.isEmpty(classGrade)) {
            return Result.error().message("请选择年级！");
        }

        Result result = sysScoreService.createCharts( classGrade, courseName,jobNum);

        return result;
    }

    @PostMapping("createLineCharts")
    @ApiOperation("生成课程学生的成绩折线图")
    public Result createLineCharts(@RequestBody CourseVo courseVo,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        String jobNum = request.getHeader("X-Token");
        String courseName = courseVo.getCourseName();
        Integer classGrade = courseVo.getClassGrade();

        if (StringUtils.isEmpty(courseName)) {
            return Result.error().message("请选课程！");
        }
        if (StringUtils.isEmpty(classGrade)) {
            return Result.error().message("请选择年级！");
        }

        Result result = sysScoreService.createLineCharts( classGrade, courseName,jobNum);

        return result;
    }


}

